<?php
namespace app\admin\controller\nursinguser;;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use think\App;
/**
 * @ControllerAnnotation(title="保姆选项")
 * Class Baomuxuanxiang
 */
class Baomuxuanxiang extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'min',
        'max',
        'money',
        'option_type_id',
        'nursing_lang_id',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    protected $sort = [
       'option_type_id'=>'desc',
       'sort'=>'desc',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\NannyOptions();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
//            echo '<pre>';
//            print_r($this->AllData['field']);
//            exit;
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            if(isset($data['nursing_lang_id'])&&$data['nursing_lang_id']){
                $nursing_lang = \app\admin\model\NursingLang::where('status',1)
                    ->where('id','in',$data['nursing_lang_id'])
                    ->select()->toArray();
            }else{
                $type = \app\admin\model\EscortType::where('status',1)
                    ->whereNotIn('types',[1,2])
                    ->column('id');
                $nursing_lang = \app\admin\model\NursingLang::where('status',1)
                    ->where('type_id','in',$type)
                    ->select()->toArray();
            }

            $nursing_lang_id = array_column($nursing_lang,'id');
            $option_type = \app\admin\model\OptionType::where('status',1)
                ->where('nursing_lang_id','in',$nursing_lang_id)
                ->select()->toArray();
            foreach ($option_type as $key=>$value){
                foreach ($nursing_lang as $v){
                    if($v['id']==$value['nursing_lang_id']){
                        if($v['id']==8){
                            $value['name'] = '短期护理-'.$value['name'];
                        }else{
                            $value['name'] = $v['name'].'-'.$value['name'];
                        }
                    }
                }
                $option_type[$key] = $value;
            }
//            echo '<pre>';
//            print_r($option_type);
//            exit;
//            foreach ($option_type)
            $this->AllData['field']['option_type_id']['option'] = $option_type;
            $this->assign('alldata',$this->AllData);
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
        $this->assign('row',$data);
        return $this->fetch();
    }
    public function edit()
    {
        $id = request()->param($this->model->getPk());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败',$post);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field'],$row);
            }
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            if(isset($data['nursing_lang_id'])&&$data['nursing_lang_id']){
                $nursing_lang = \app\admin\model\NursingLang::where('status',1)
                    ->where('id','in',$data['nursing_lang_id'])
                    ->select()->toArray();
            }else{
                $type = \app\admin\model\EscortType::where('status',1)
                    ->whereNotIn('types',[1,2])
                    ->column('id');
                $nursing_lang = \app\admin\model\NursingLang::where('status',1)
                    ->where('type_id','in',$type)
                    ->select()->toArray();
            }

            $nursing_lang_id = array_column($nursing_lang,'id');
            $option_type = \app\admin\model\OptionType::where('status',1)
                ->where('nursing_lang_id','in',$nursing_lang_id)
                ->select()->toArray();
            foreach ($option_type as $key=>$value){
                foreach ($nursing_lang as $v){
                    if($v['id']==$value['nursing_lang_id']){
                        if($v['id']==8){
                            $value['name'] = '短期护理-'.$value['name'];
                        }else{
                            $value['name'] = $v['name'].'-'.$value['name'];
                        }
                    }
                }
                $option_type[$key] = $value;
            }
//            echo '<pre>';
//            print_r($option_type);
//            exit;
//            foreach ($option_type)
            $this->AllData['field']['option_type_id']['option'] = $option_type;
//            echo '<pre>';
//            print_r($this->AllData['field']['my_lselect']);
//            exit;
            $this->assign('alldata',$this->AllData);
        }
        return $this->fetch();
    }
}